<?php ob_start();session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
} ?>
<html>
<head>
	<title>Price Checking</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
	 <link href="select2/select2.css" rel="stylesheet"/>
	<script src="select2/select2.js"></script>
	<script src="jquery_session/jquery.session.js"></script>
	<script>
	$(document).ready(function() { 
		$("#product").select2({
		placeholder: "Select Shop",
		allowClear: true
		});
		
		$("#color").select2({
		placeholder: "Select Color",
		allowClear: true
		});
		
		$("#price").priceFormat({
        prefix: "",
        centsLimit: 0,
        centsSeparator: "",
        thousandsSeparator: ","
	
		});
	});
	$(document).on("click", "#showproduct a", function(){
		$("#showproduct a").removeClass("selected");
		$("#showproduct a").removeAttr("style");
		$(this).addClass("selected");
	});

	//$(".select2-results li").attr({"background-color","red"});
	
	 
	//node.addClass("selected");
	</script>
</head>

<body>

<script>

	function changeshop(id)
		{
		var result=	$('[name="collect"]:radio:checked').val();
		var xmlhttp;
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			//document.getElementById("showurl").innerHTML="";
			//document.getElementById("price").innerHTML="";
			document.getElementById("showproduct").innerHTML=xmlhttp.responseText;
			
		
			}
		  }
		xmlhttp.open("GET","changeshop.php?id_shop="+id+"&result="+result,true);
		xmlhttp.send();
		}
	
		function getqty0(id)
			{
			var result=	$('[name="collect"]:radio:checked').val();
			var xmlhttp;
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
				
				document.getElementById("getqty0").innerHTML=xmlhttp.responseText;
				}
			  }
			xmlhttp.open("GET","getqty0.php?qty="+id,true);
			xmlhttp.send();
			}
		function getqty1(id)
			{
			var result=	$('[name="collect"]:radio:checked').val();
			var xmlhttp;
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
				
				document.getElementById("getqty1").innerHTML=xmlhttp.responseText;
				}
			  }
			xmlhttp.open("GET","getqty1.php?qty="+id,true);
			xmlhttp.send();
			}
		function getqty2(id)
			{
			var result=	$('[name="collect"]:radio:checked').val();
			var xmlhttp;
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
				
				document.getElementById("getqty2").innerHTML=xmlhttp.responseText;
				}
			  }
			xmlhttp.open("GET","getqty2.php?qty="+id,true);
			xmlhttp.send();
			}
	
	function changestatus()
		{
		var result=	$('[name="collect"]:radio:checked').val();
		var id=	$('#product').val();
		var xmlhttp;
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			
			document.getElementById("showproduct").innerHTML=xmlhttp.responseText;
		
			}
		  }
		xmlhttp.open("GET","changeshop.php?result="+result+"&id_shop="+id,true);
		xmlhttp.send();
				
		}
</script>
<style>
.select2-results li {
    list-style: none;
    display: list-item;
    background-image: none;
}
.checked {background-color:#F5F5F5;}
body{margin:0px}
.styled-select select {font-size: 16px;line-height: 26px;border: 0;border-radius: 0px;-webkit-appearance: none;  }
.styled-select select:hover{background:#f5f5f5}
.styled-select {  overflow: hidden; width:100%; border: 1px solid #ddd;}
.parent{width:98%;margin: 0px auto; padding:1%}
#showurl{width:60%; min-width:60%;float:left;}
#showweb{float:right;width:38%;}
/* The CSS */
select {padding:3px;background: #f8f8f8;color:#888;border:none;outline:none;display: inline-block;-webkit-appearance:none;-moz-appearance:none;appearance:none;
cursor:pointer; width:100%; margin:0px }
option{line-height:24px !important; margin:5px 0px}
@media screen and (-webkit-min-device-pixel-ratio:0) { select {padding-right:18px}}
selectbox {position:relative;}
selectbox:after {content:'<>';font:11px "Consolas", monospace;color:#aaa; -webkit-transform:rotate(90deg);-moz-transform:rotate(90deg);-ms-transform:rotate(90deg);transform:rotate(90deg);right:8px; top:2px;padding:0 0 2px;
border-bottom:1px solid #ddd;position:absolute;pointer-events:none;}
selectbox:before {content: "";display: block;height: 27px;pointer-events: none;position: absolute;right: 0;top: -4px;width: 22px;}
.linkshop{text-decoration: none;color: #09F;display: inline-block;width: 100%;padding: 3px 0px; border-bottom:1px dotted #CCC}
.linkshop:hover { background:#0CF; color:#FFF;}
.selected {background:#0CF;color:white}
.changed {background:red;color:white}
table td{color:#333;}
table input{
    border: 1px solid #848484; 
    -webkit-border-radius: 5px; 
    -moz-border-radius: 5px; 
    border-radius: 5px; 
    outline:0; 
    height:30px; 
    padding-left:10px; 
    padding-right:10px; 
  } 
.url{float:right; margin-bottom:15px; border:1px solid #ddd; border-right: none; border-radius:5px; padding:10px}
.disable{color:#999999 !important}
.disa{border: 1px solid #999999 !important; color:#999999 !important}
.change{-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; padding:5px 58px; height:auto; background:#0cf; color:#fff; font-size:16px; border: 0px solid #0066FF; }
.change:hover{background:#09f; cursor:pointer; }
.writer {color: #ff5f00;}
.logout {color:black;border:1px solid black; padding:3px;text-decoration:none;font-size:14px}
.logout:hover  {background-color:#ff5f00; color:#FFF; border:1px solid #ff5f00}
input[type=checkbox]
{
  /* Double-sized Checkboxes */
  -ms-transform: scale(1.2); /* IE */
  -moz-transform: scale(1.2); /* FF */
  -webkit-transform: scale(1.2); /* Safari and Chrome */
  -o-transform: scale(1.2); /* Opera */
  padding: 10px;
  height:auto;
}
#showproduct{border:1px #ddd solid; border-top:none; width:100%;}
.styled-select select > option {color:red}
</style> 

<?php if(isset($_SESSION['accountshopping'])){ ?>
<div class="parent">
	<div>
		<iframe id="showurl" width="100%" height="98%" frameborder="0"></iframe>
	</div>

	<div id="showweb">
	<div style="float:right;"><span class="writer" style="padding-right:10px"><?php echo $_SESSION['accountshopping'][1]; ?></span><a href="logout.php" onclick="return confirm('Are you sure ?');" class="logout">Sign Out</a></div>
	<form style="margin: 25px 0px 5px 0px;" method="POST" name="status" onchange="changestatus()">
	<?php
		include "connection_open.php";
		
		$result=mysql_query('select * from product group by price_result'); 
		while($row_price=mysql_fetch_array($result))
		{ 
			if($row_price['price_result']!=3 && $row_price['price_result']!=4 && $row_price['price_result']!=5)
			{
	?>	
		<span class="writer"><label><input type="radio" id="collect" name="collect" value="<?=$row_price['price_result'];?>" checked="checked"/><?php if($row_price['price_result']==2){ echo "Thu thập [Khác]";?><span id="getqty2"></span><?php  } if($row_price['price_result']==1){ echo "Thu thập [Giống]";?><span id="getqty1"></span><?php  } if($row_price['price_result']==0 || $row_price['price_result']==3 || $row_price['price_result']==4 || $row_price['price_result']==5){ echo "Không thu thập được";?><span id="getqty0"></span><?php } ?></label></span>
	<?php 
			}
		} 
		include "connection_close.php";
	?>
	</form>
	
	<div class="styled-select">

			<select id="product" style="width:100%" onchange="changeshop(this.value);getqty0(this.value);getqty1(this.value);getqty2(this.value)">

			<option value=""></option>
        
	<?php
		include "connection_open.php";
		$query=mysql_query("select * from shop order by name ASC");
		
		while($row=mysql_fetch_array($query))
		{	
			$qty0=mysql_fetch_array(mysql_query("select count(*) as count from product where id_shop='".$row['id']."' and (price_result=0 or price_result=3 or price_result=4 or price_result=5) AND `status`=1 and quantity=1"));
			$count0=$qty0['count'];
			$qty1=mysql_fetch_array(mysql_query("select count(*) as count from product where id_shop='".$row['id']."' and (price_result=1 or price=price_new) AND `status`=1 and quantity=1"));
			$count1=$qty1['count'];
			$qty2=mysql_fetch_array(mysql_query("select count(*) as count from product where id_shop='".$row['id']."' and price_result=2 and price!=price_new AND `status`=1 and quantity=1"));
			$count2=$qty2['count'];
	?>
				<option class="shop <?php if($count2==0) echo "checked"; else echo ""; ?>" value="<?=$row['id'];?>"><?=$row['name'];?>&nbsp;(<?=$count0?>-<?=$count1?>-<?=$count2?>)</option>
	<?php				
		}
		
	?>		
			</select>
	</div>
		
		<div id="showproduct">
		
		<script>
		function check()
			{
				nn= document.n;
				if(nn.newprice.value=='')
				{
					alert('New price not set yet');
					nn.newprice.focus();
					return;
				}
				else
				{
					var oldprice=nn.oldprice.value;
					var product=nn.product.value;
					var newprice=nn.newprice.value;
					var outofstock=$('.outofstock:checked').val();
					var string= '';
					$('input:checkbox[name="color[]"]').each(function() {
						if (this.checked) {
							
							string+=this.value+"-";
						}
					});
					var string_size= '';
					$('input:checkbox[name="size[]"]').each(function() {
						if (this.checked) {
							
							string_size+=this.value+"-";
						}
					});
					
					

					 var xmlhttp;
					if (window.XMLHttpRequest)
					  {// code for IE7+, Firefox, Chrome, Opera, Safari
					 xmlhttp=new XMLHttpRequest();
					  }
					else
					  {// code for IE6, IE5
					  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
					  }
					xmlhttp.onreadystatechange=function()
					{
					  if (xmlhttp.readyState==4 && xmlhttp.status==200)
					  {
						
						var id=document.getElementById("id_shop").value;
						if(outofstock=="on"){
						alert('Update success ! Product`s price was changed from '+oldprice+'원 to '+newprice+'원 and set to out of stock !');
						//Xu ly load trang
							
						}else{
						alert('Update success ! Product`s price was changed from '+oldprice+'원 to '+newprice+'원 !');
						}
						keep(id);
						//changeshop(id);
						getqty1(id);
						getqty0(id);
						getqty2(id);
						var product_next=xmlhttp.responseText;
						
						if(product_next!='')
						{					
							showurl(product_next);
							showprice(product_next);
						}
						else
						{	
							document.getElementById("showurl").src="";
							document.getElementById("price").innerHTML="";	
							changeshop(id);
						}
					  }
					}  
					 if(outofstock=="on")
					xmlhttp.open("GET","changeprice.php?product="+product+"&outofstock="+outofstock+"&newprice="+newprice+"&color="+string+"&size="+string_size,true);
					else
					xmlhttp.open("GET","changeprice.php?product="+product+"&newprice="+newprice+"&color="+string+"&size="+string_size,true);
					xmlhttp.send(); 
					
			
				}	
			}
		
		function showurl(id){
			document.getElementById("showurl").src="showurl1.php?id="+id;
			
				/*	var xmlhttp;
				if (window.XMLHttpRequest)
				  {// code for IE7+, Firefox, Chrome, Opera, Safari
				  xmlhttp=new XMLHttpRequest();
				  }
				else
				  {// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				  }
				xmlhttp.onreadystatechange=function()
				  {
				  if (xmlhttp.readyState==4 && xmlhttp.status==200)
					{
					document.getElementById("showurl").innerHTML=xmlhttp.responseText;
					}
				  }
				xmlhttp.open("GET","showurl1.php?id="+id,true);
				xmlhttp.send();*/

			}
		function showprice(id)
			{
				var xmlhttp;
				if (window.XMLHttpRequest)
				  {// code for IE7+, Firefox, Chrome, Opera, Safari
				  xmlhttp=new XMLHttpRequest();
				  }
				else
				  {// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				  }
				xmlhttp.onreadystatechange=function()
				  {
				  if (xmlhttp.readyState==4 && xmlhttp.status==200)
					{
					document.getElementById("price").innerHTML=xmlhttp.responseText;

					}
				  }
				xmlhttp.open("GET","price.php?id="+id,true);
				xmlhttp.send();
			}
		</script>	
	
		</div>
		<div id="price" style="margin-top:5px">
		<script>
			function keep(id)
			{
				var name= document.n.product.value;
				var result=	$('[name="collect"]:radio:checked').val();
				var xmlhttp;
				if (window.XMLHttpRequest)
				  {// code for IE7+, Firefox, Chrome, Opera, Safari
				  xmlhttp=new XMLHttpRequest();
				  }
				else
				  {// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				  }
				xmlhttp.onreadystatechange=function()
				  {
				  if (xmlhttp.readyState==4 && xmlhttp.status==200)
					{
					
					//document.getElementById("showurl").innerHTML="";
					//document.getElementById("price").innerHTML="";
					document.getElementById("showproduct").innerHTML=xmlhttp.responseText;		
			
					}
				  }
				xmlhttp.open("GET","keep.php?id_shop="+id+"&result="+result+"&id="+name,true);
				xmlhttp.send();
			}
			function huybo()
			{
				
					/* var oldprice=nn.oldprice.value;
					var product=nn.product.value;
					var newprice=nn.newprice.value;
					var outofstock=$('.outofstock:checked').val();
					var string= '';
					$('input:checkbox[name="color[]"]').each(function() {
						if (this.checked) {
							
							string+=this.value+"-";
						}
					}); */
					nn=document.n;
					var product=nn.product.value;
					 var xmlhttp;
					if (window.XMLHttpRequest)
					  {// code for IE7+, Firefox, Chrome, Opera, Safari
					 xmlhttp=new XMLHttpRequest();
					  }
					else
					  {// code for IE6, IE5
					  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
					  }
					xmlhttp.onreadystatechange=function()
					{
					  if (xmlhttp.readyState==4 && xmlhttp.status==200)
					  {
						
						var product_next=xmlhttp.responseText;
					
						var id=document.getElementById("id_shop").value;
						keep(id);
						//changeshop(id);
						getqty1(id);
						getqty0(id);
						getqty2(id);
						alert($.session.get("scroll"));
						if(product_next!='')
						{					
							showurl(product_next);
							showprice(product_next);							
						}
						else
						{	
							document.getElementById("showurl").src="";
							document.getElementById("price").innerHTML="";	
							changeshop(id);
						}
						
					  }
					}  
					xmlhttp.open("GET","huybo.php?product="+product,true);
					xmlhttp.send(); 
					
			
				
			}
		</script>
		</div>
	</div>
</div>
	
<?php
	}
	else
	{
		header("Location: login");
	}
	include "connection_close.php";
?>
</body>
</html>
<?php ob_flush(); ?>